package com.producteev.version0.test;

import com.producteev4j.exceptions.ProducteevException;
import com.producteev4j.exceptions.ProducteevServiceException;
import com.producteev4j.model.response.Note;
import com.producteev4j.model.response.Task;
import com.producteev4j.model.response.UserLogin;
import com.producteev4j.services.ProducteevService;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: jcarrey
 * Date: 14/05/11
 * TimeImpl: 17:07
 * To change this template use File | Settings | File Templates.
 */
public class ITTestTaskProducteevServices extends TestCase {

    private static ProducteevService service;

    @Before
    public void setUp() {
        if (service == null) {
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-test.xml");
            service = (ProducteevService) applicationContext.getBean("service");
        }
    }


    private static final String EMAIL = "juankarrey@gmail.com";
    private static final String PASSWORD = "pr0duc733v4j";

    private static final long ID_DASHBOARD = 146402;
    private static final long TASK_ID = 3129167L;
    private static final long USER_ID = 107379L;
    private static final long LABEL_ID = 337263L;
    private static final long NOTE_ID = 1859814L;
    private static final long ACTIVITY_ID = 18017080L;

    private static String userToken;
    private static Task task;
    private static Note note;


    @Test
    public void testUserLogin() throws ProducteevException {
        UserLogin userLogin = service.userLogin(EMAIL, PASSWORD);
        userToken = userLogin.getToken();
    }


    @Test
    public void testCreateTask() throws ProducteevException {
        task = service.createTask(userToken, "Title of the task ! ", null, ID_DASHBOARD, null, null, null, null, null, null, null, new Long[]{LABEL_ID});
    }


    @Test
    public void testGetTasks2() throws ProducteevException {
        // 1 year
        Date time = new Date();
        time.setTime(time.getTime() - 31536000000L);
        service.getTasks(userToken, time, ID_DASHBOARD, null);
    }

    @Test
    public void testGetTasks() throws ProducteevException {
        service.getTasks(userToken, null, null, 1);
    }

    @Test
    public void testGetTask() throws ProducteevException {
        service.getTask(userToken, TASK_ID);

    }


    @Test
    public void testGetTaskAccessControlList() throws ProducteevException {
        service.getTaskAccessControlList(userToken, TASK_ID);
    }


    @Test
    public void testSetTaskRepeating() throws ProducteevException {
        try {
            Date tomorrow = new Date();
            tomorrow.setTime(tomorrow.getTime() + 60 * 24 * 60 * 1000);
            service.setTaskDeadline(userToken, TASK_ID, tomorrow, true);
        } catch (ProducteevException pe) {
            Assert.fail("Needs setTaskDeadline working for test setTaskRepeating");
        }
        service.setTaskRepeating(userToken, TASK_ID, "day", 1);
    }

    @Test
    public void testUnsetTaskRepeating() throws ProducteevException {
        service.unsetTaskRepeating(userToken, TASK_ID);
    }

    @Test
    public void testsetTaskReminder() throws ProducteevException {
        service.setTaskReminder(userToken, TASK_ID, 1);
    }


    @Test
    public void testsetTaskDeadline() throws ProducteevException {
        service.setTaskDeadline(userToken, TASK_ID, new Date(), false);
    }

    @Test
    public void testUnsetTaskDeadline() throws ProducteevException {
        service.unsetTaskDeadline(userToken, TASK_ID);
    }


    @Test
    public void testsetTaskResponsible() throws ProducteevException {
        service.setTaskResponsible(userToken, TASK_ID, USER_ID);
    }

    @Test
    public void testUnsetTaskResponsible() throws ProducteevException {
        service.unsetTaskResponsible(userToken, TASK_ID, USER_ID);
    }


    @Test
    public void testsetTaskStars() throws ProducteevException {
        service.setTaskStars(userToken, TASK_ID, 3);
    }

    @Test
    public void testsetTaskTitle() throws ProducteevException {
        service.setTaskTitle(userToken, TASK_ID, "New title");
    }

    @Test
    public void testsetTaskStatus() throws ProducteevException {
        service.setTaskStatus(userToken, TASK_ID, 2);
    }

    @Test
    public void testGetArchivedTasks() throws ProducteevException {
        service.getArchivedTasks(userToken, null, ID_DASHBOARD, null);
    }


    @Test
    public void testMyTasks() throws ProducteevException {
        service.getMyTasks(userToken, null, null, null);
    }

    @Test
    public void testMyTeamTasks() throws ProducteevException {
        service.getMyTeamTasks(userToken, null, null, null);
    }

    @Test
    public void testGetTaskLabels() throws ProducteevException {
        service.getTaskLabels(userToken, TASK_ID);
    }

    @Test
    public void testChangeLabels() throws ProducteevException {
        service.changeTaskLabels(userToken, TASK_ID, new Long[]{LABEL_ID});
    }

    /*  TODO - NotFound
    @Test
    public void testTaskUserList() throws ProducteevException {
        service.getTaskUserList(userToken, TASK_ID);
    } */

    @Test
    public void testSetTaskWorkspace() throws ProducteevException {
        service.setTaskWorkspace(userToken, TASK_ID, ID_DASHBOARD);
    }


    @Test
    public void testNote() throws ProducteevException {
        service.getNote(userToken, NOTE_ID);
    }

    @Test
    public void testDeleteTask() throws ProducteevException {
        service.deleteTask(userToken, task.getIdTask());
    }

    @Test
    public void testGetTaskNotes() throws ProducteevException {
        service.getTaskNotes(userToken, TASK_ID, new Date());

    }


    @Test
    public void testgetTaskActivity() throws ProducteevException {
        service.getTaskActivity(userToken, TASK_ID, 1);
    }

    @Test
    public void testgetActivity() throws ProducteevException {
        service.getActivity(userToken, ACTIVITY_ID);
    }

    @Test
    public void testcreateNote() throws ProducteevException {
        note = service.createNote(userToken, TASK_ID, "Wharapity !");
    }

    @Test
    public void testdeleteNote() throws ProducteevException {
        if (note == null) {
            throw new ProducteevServiceException("Cannot test delete note");
        }
        service.deleteNote(userToken, note.getIdNote());
    }
}
